define('order/query', function() {
	var ViewModel = function() {
		var self = this;
		// 第一个参数请求路径，第二个是传入的参数，第三个是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax("/orderhead/doFindObject", 20, self.finMenu);
		self.finMenu = function(result) {
			console.log(result);
			var data = result;
			if (data && data.length && data.length > 0) {
				$.each(data, function(index, val) {
					val.checked = false;
				});
			}
			var items = ko.mapping.fromJS(data);
			return items();
		}
		self.file = ko.observable({
			orderid : '',
			batchnumber : '',
			expressnumber : '',
			start : ko.observable(''),
			end : ko.observable('')
		});
		// 清空
		self.empty = function() {
			self.file({
				orderid : '',
				batchnumber : '',
				expressnumber : '',
				start : '',
				end : ''
			});
		}
		/** 查询（查询按钮） */
		self.search = function() {
			self.file().start = $("#startDate").val();
			self.file().end = $("#endDate").val();
			var data = self.file();
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);
		};
		self.search();
		// 查询回执信息
		self.receipt = function(orderhead) {
			$.ajax({
				url : '/orderhead/doFindReceipt',
				method : 'POST',
				contentType : 'application/json',
				data : JSON.stringify(orderhead),
				success : function(result) {
					if (result.status == 0) {
						// console.log(result.data.content)
						$("#content").append().val(result.data);
						layer.open({
							type : 1,
							title : "查看回执信息",
							area : [ '560px', '560px' ],
							content : $('#pop_div_receipt')
						});
					} else {
						toastr.error("查询失败");
					}
				}
			});
		}
		// 关闭窗口
		self.layerClose = function() {
			layer.close(layer.index);
			toastr.info("已关闭");
		};

		// 发送数据
		self.send = function(data) {
			layer.confirm('确定要重推订单？', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				var orderid = data.orderid;
				console.log(orderid)
				$.ajax({
					url : '/orderhead/send',
					method : 'POST',
					contentType : 'application/json',
					data : JSON.stringify(orderid),
					success : function(result) {
						layer.closeAll();
						toastr.success(result);
						console.log(result);
					},
				});
			})
		};
		 //修改orderhead模板
		self.update_model = ko.observable({
			orderid : '',
			ordersum : '',
			modifymark : '',
			billmode : '',
			licenseno : '',
			chargetype : '',
			paynumber : '',
			ietype : '',
			declname : '',
			collectionusername : '',
			collectionuseraddress:'',
			goodsvalue : '',
			declcode : '',
			cbecodeinsp : '',
			sendusername : '',
			sendusercountry : '',
			collusercountryinsp : '',
			cbename : '',
			cbecode : '',
			coin:'',
			ecpcode:'',
			cbecodeinsp:'',
			collectionuseraddress : '',
			sendusercountryinsp : ''
		});
		// 弹出修改对话框
		self.update = function(data) {
			layer.open({
				type : 1,
				title : "修改订单信息",
				area : [ '1200px', '500px' ],
				content : $('#div_update')
			});
			// 根据Id显示订单信息
			self.getOrderHead(data);
		    self.getOrderList(data);
		}
		//根据ID获取orderhead
		self.getOrderHead = function(data) {
			$.ajax({
				url : '/orderhead/findOrderHeadByOrderId/'+data.orderid,
				method : 'POST',
				contentType : 'application/json',
				data : JSON.stringify(data.orderid),
				success : function(result) {
					 console.log(result);
					self.update_model(result);
				},
			});
		}
		//根据ID获取orderlist
		self.orderlist_arr=ko.observable([]);
		self.getOrderList = function(data) {
			$.ajax({
				url : '/orderhead/findOrderListByOrderId/'+data.orderid,
				method : 'POST',
				contentType : 'application/json',
				data : JSON.stringify(data.orderid),
				success : function(result) {
					console.log(result.result);
					self.orderlist_arr(result.result);
				},
			});
		}
		
		// 保存
		self.update_save = function() {
		     self.updateOrderHead();
		     self.updateOrderList();
		}
      
		
		//保存orderhead
		self.updateOrderHead = function() {
			 var data = self.update_model();
			 console.log(data);
			$.ajax({
				url : '/orderhead/save_orderhead',
				method : 'POST',
				contentType : 'application/json',
				data : JSON.stringify(data),
				success : function(result) {
					console.log(result);
					if (result.status == 0) {
						layer.closeAll();
						toastr.success(result.msg)
					} else {
						toastr.error("保存orderhead失败");
					}

				},
			});
		}
		// 保存orderList
		self.updateOrderList = function() {
			var data =self.orderlist_arr();
			console.log(data);
			$.ajax({
				url : '/orderhead/save_orderlist',
				method : 'POST',
				contentType : 'application/json',
				data : JSON.stringify(data),
				success : function(result) {
					console.log(result);
					if (result.status == 0) {
						layer.closeAll();
						toastr.success(result.msg)
					} else {
						toastr.error("保存orderlist失败");
					}
				},
			});
		}
	
	}
	var model = new ViewModel();
	ko.applyBindings(model);
});